jQuery(function(event) {
	var lastMappedSystemCityId = 0;

	$('#locationsList input[type="button"].butChange').click(function(e){
		var butChange = $(this);
		butChange.hide();
		var tdButtonChange = butChange.parent();
		var tds = tdButtonChange.parent().children();
		
		var butNew = tds.eq(5).children('.butNew');
		butNew.attr('disabled', 'disabled');
		
		var vendorCityName = tds.eq(2).text();
		var tdSysCityTranslation = tds.eq(6);
		var oldSysCityTranslation = tdSysCityTranslation.text();
		var tdSysCityName = tds.eq(7);
		var tdSysCityCode = tds.eq(8);
 		
		tdSysCityTranslation.text('');
		var selectMapSysCities = $('select.selectMapSysCities').clone().removeClass().appendTo(tdSysCityTranslation).show();
		selectMapSysCities.val(tdSysCityCode.text() ? tdSysCityCode.text() : lastMappedSystemCityId);

		var funCloseChangeMapping = function(e){
			funEnableControls();
			tdButtonChange.children().not('.butChange').remove();
			butChange.show();
			butNew.removeAttr('disabled');
		};
		
		var funEnableControls = function(e){
			tds.eq(4).find('input,select').removeAttr('disabled');
			tdSysCityTranslation.find('input,select').removeAttr('disabled');
		};
		
		$('<input class="button-ok" type="button" value="' + 'BTN_OK'.translate() + '" />').click(function(e){
			var newSysCityCode = selectMapSysCities.val();
			if(newSysCityCode.length == 0)
			{
				return;
			}
			tds.eq(4).find('input,select').attr('disabled', 'disabled');
			tdSysCityTranslation.find('input,select').attr('disabled', 'disabled');
			$.ajax({
				url: '/a_admin/location/translation.setVendorCityMapping',
				method: 'post',
				timeout: 20000,
				dataType: 'json',
				async: false,
				data: {
					vendorCityName: vendorCityName,
					systemCityCode: newSysCityCode
				},
				success: function(data){
					if(data.status != undefined && data.status == 'success')
					{
						data = data.data;
						tdSysCityTranslation.text(data.newSysCityTranslation);
						tdSysCityName.text(data.newSysCityName);
						tdSysCityCode.text(data.newSysCityCode);
						var vendorCityNameLowerCase = vendorCityName.toLowerCase();
						$('#locationsList').find('tr').each(function(){
							var trtds = $(this).find('td');
							if(trtds.eq(2).text().toLowerCase() == vendorCityNameLowerCase)
							{
								trtds.eq(6).removeAttr('style').text(data.newSysCityTranslation);
								trtds.eq(7).removeAttr('style').text(data.newSysCityName);
								trtds.eq(8).removeAttr('style').text(data.newSysCityCode);
							} 
						});
						lastMappedSystemCityId = data.newSysCityCode; 
						funCloseChangeMapping();
					}
					else
					{
						alert('Mapping saving error!');
						funEnableControls();
					}							
				},
				error: function(){
					alert('Mapping saving error!');
					funEnableControls();
				}
			});
		}).appendTo(tdButtonChange);
		
		$('<input class="button-cancel" type="button" value="' + 'BTN_CANCEL'.translate() + '" />').click(function(){
			tdSysCityTranslation.text(oldSysCityTranslation);
			funCloseChangeMapping();
		}).appendTo(tdButtonChange);
		
	});

	$('#locationsList input[type="button"].butNew').click(function(e){
		var butNew = $(this);
		butNew.hide();
		var tdButtonNew = butNew.parent();
		var tds = tdButtonNew.parent().children();
		var tdButtonChange = tds.eq(4);
		var butChange = tdButtonChange.children('.butChange');
		butChange.attr('disabled', 'disabled');
		
		var vendorCityName = tds.eq(2).text();
		var tdSysCityTranslation = tds.eq(6);
		var oldSysCityTranslation = tdSysCityTranslation.text();
		var tdSysCityName = tds.eq(7);
		var oldSysCityName = tdSysCityName.text();
		var tdSysCityCode = tds.eq(8);
		var oldSysCityCode = tdSysCityCode.text();
		
		tdSysCityTranslation.text('');
		var inputNewSysCityTranslation = $('<input type="text" />')
			.appendTo(tdSysCityTranslation)
		;
		
		tdSysCityName.text('');
		var inputNewSysCityName = $('<input type="text" />')
			.val(vendorCityName)
			.appendTo(tdSysCityName)
		;
		
		var funEnableControls = function(e){
			tds.eq(5).find('input,select').removeAttr('disabled');
			tdSysCityTranslation.find('input,select').removeAttr('disabled');
			tdSysCityName.find('input,select').removeAttr('disabled');
		};

		var funCloseNewMapping = function(e){
			funEnableControls();
			tdButtonNew.children().not('.butNew').remove();
			tdButtonChange.children().not('.butChange').remove();
			inputNewSysCityTranslation.remove();
			inputNewSysCityName.remove();
			butNew.show();
			butChange.removeAttr('disabled');
		};

		tds.find('input[type="text"]').css('border', '2px solid red');
		
		$('<input class="button-ok" type="button" value="' + 'BTN_OK'.translate() + '" />').click(function(e){
			if(inputNewSysCityName.val().replace(/[\s\r\n\t]+/, '').length == 0 || inputNewSysCityTranslation.val().replace(/[\s\r\n\t]+/, '').length == 0)
			{
				return;
			}
			tds.eq(5).find('input,select').attr('disabled', 'disabled');
			tdSysCityTranslation.find('input,select').attr('disabled', 'disabled');
			tdSysCityName.find('input,select').attr('disabled', 'disabled');
			$.ajax({
				url: '/a_admin/location/translation.newVendorCityMapping',
				method: 'post',
				timeout: 20000,
				dataType: 'json',
				async: false,
				data: {
					vendorCityName: vendorCityName,
					systemCityTranslation: inputNewSysCityTranslation.val(),
					systemCityName: inputNewSysCityName.val()
				},
				success: function(response){
					if(response.status != undefined && response.status == 'success')
					{
						data = response.data;
						tdSysCityName.text(data.newSysCityName);
						tdSysCityTranslation.text(data.newSysCityTranslation);
						tdSysCityCode.text(data.newSysCityCode);
						var vendorCityNameLowerCase = vendorCityName.toLowerCase();
						$('#locationsList').find('tr').each(function(){
							var trtds = $(this).find('td');
							if(trtds.eq(2).text().toLowerCase() == vendorCityNameLowerCase)
							{
								trtds.eq(6).removeAttr('style').text(data.newSysCityTranslation);
								trtds.eq(7).removeAttr('style').text(data.newSysCityName);
								trtds.eq(8).removeAttr('style').text(data.newSysCityCode);
							} 
						});
						jQuery('<option>').val(data.newSysCityCode).text(data.newSysCityTranslation + ' [' + data.newSysCityName + ']').insertAfter('select.selectMapSysCities option:first');
						funCloseNewMapping();
						if(response.message != undefined)
						{
							alert(response.message);
						}
					}
					else
					{
						alert('Mapping saving error!');
						funEnableControls();
					}							
				},
				error: function(){
					alert('Mapping saving error!');
					funEnableControls();
				}
			});
		}).appendTo(tdButtonNew);
		
		$('<input class="button-cancel" type="button" value="' + 'BTN_CANCEL'.translate() + '" />').click(function(){
			tdSysCityTranslation.text(oldSysCityTranslation);
			tdSysCityName.text(oldSysCityName);
			funCloseNewMapping();
		}).appendTo(tdButtonNew);
		
	});

	$('#locationsList input[type="checkbox"].butVisibility').change(function(e){
		var butVisibility = $(this);
		var vendorCityCode = butVisibility.parent().parent().children().eq(1).text();
		
		var funRevertVisibility = function(e){
 			if(e.attr('checked'))
			{
				e.removeAttr('checked');
				e.next('a').find('img').removeClass('checked');
			}
			else
			{
				e.next('a').find('img').addClass('checked');
				e.attr('checked', 'checked');
			}
		};
		
		$.ajax({
			url: '/a_admin/location/translation.changeVendorCityVisibility',
			method: 'post',
			timeout: 20000,
			dataType: 'json',
			async: false,
			data: {
				vendorCityCode: vendorCityCode,
				vendorId: butVisibility.val(),
				visible: (butVisibility.attr('checked') ? 'Y' : 'N')
			},
			success: function(data){
				if(data.status != undefined)
				{
					if(data.status == 'success')
					{
					}
					else
					{
						alert('Visibility changing error: ' + data.message);
						funRevertVisibility(butVisibility);
					}
				}
				else
				{
					alert('Visibility changing error!');
					funRevertVisibility(butVisibility);
				}							
			},
			error: function(){
				alert('Visibility changing error!');
				funRevertVisibility(butVisibility);
			}
		});
	});
});
